#! /usr/bin/perl -w
#use strict;
use utf8;
use Gtk2::TrayIcon;
use Gtk2 -init;
use Glib qw/TRUE FALSE/;

## WARNING, IF MARBLE ISN'T IN YOUR APP PATH, IT WONT APPEAR IN TRAY MENU ##
### CHANGE ME ####
$user = "/home/mike";
$dirs = "$user/Documents/CLIM";
$bin_path = "/usr/local/bin/";
##################

$icon = "$dirs/.icons";

$command01 = ('h_track -p');    # check ponctually area
$command02 = ('h_track -iw');   # dl and view last sat image (eog)
$command03 = ('h_track -k');	#check cron state

$command04 = ("nautilus $dirs");
$command05 = ("h_track.pl");
#$command05 = ("nautilus $sat");
#$command06 = ("nautilus $arch");
$command07 = ("marble");

#$command08 = ('h_track -c');    #	start cron check
#$command09 = ('h_track -di');	# dl sat image daily in background
#$command10 = ('h_track -hi');	# dl sat image hourly in background
#$command11 = ('h_track -s');	# stop and remove user cron file
#$command12 = ('h_track -r');	# reset html data if download error

$tempmsg00 = ("$user/.MSG.txt");
$tempmsg01 = ("$user/.MSG1.txt");
$tempmsg02 = ("$user/.MSG2.txt");
$warnmsg = "ERREUR DANS LA LECTURE DU MESSAGE";

#my $flag = 1;

my $cmdtray= Gtk2::TrayIcon->new('toplevel');
$cmdtray->signal_connect('delete_event' => sub { Gtk2->main_quit; });
my $cmdbox = &mvbox();
$cmdtray->add($cmdbox);
$cmdtray->show;
Gtk2->main ();
sub mvbox {
        my $cmdbox = Gtk2::VBox->new(0,0);
                my $cmdlist = Gtk2::Menu->new ();
                my $cmd01 = Gtk2::ImageMenuItem->new('Contrôle de la zone');
                my $cmd02 = Gtk2::ImageMenuItem->new('Voir dernier message');
                my $cmd03 = Gtk2::ImageMenuItem->new('Voir dernier bulletin');
                my $cmd04 = Gtk2::ImageMenuItem->new('Charger Image Sat');
                my $cmd05 = Gtk2::ImageMenuItem->new('Contrôle du timer');
                my $cmd06 = Gtk2::ImageMenuItem->new('Répertoire des Dossiers');
                my $cmd07 = Gtk2::ImageMenuItem->new('Ouvrir le tracker');
                #my $cmd07 = Gtk2::ImageMenuItem->new('');
                #$count = (`cat -n "$tempmsg02"|awk \'\{print \$1\}\'\|sed -n \'\$p\'`);
                #&advice;
                $icon01 = Gtk2::Image->new_from_stock('gtk-ok','large-toolbar');
                $cmd01->signal_connect('activate', sub {
                        system ($command01);
                        &advice;
                        &msgbox1;
                        });
                $cmd01->set_image($icon01);
                $cmdlist->append($cmd01);
                $cmdlist->append(Gtk2::SeparatorMenuItem->new());
                $icon02 = Gtk2::Image->new_from_stock('gtk-dialog-warning','large-toolbar');
                $cmd02->signal_connect('activate', sub {
                        $advisory = $tempmsg01;
                        &msgbox1;
                        });
                $cmd02->set_image($icon02);
                $cmdlist->append($cmd02);
                $icon03 = Gtk2::Image->new_from_stock('gtk-copy','large-toolbar');
                $cmd03->signal_connect('activate', sub {
                        #$advisory = $tempmsg02;
                        &advice;
                        &msgbox1;
                        });
                $cmd03->set_image($icon03);
                $cmdlist->append($cmd03);
                $cmdlist->append(Gtk2::SeparatorMenuItem->new());
                $icon04 = Gtk2::Image->new_from_stock('gtk-refresh','large-toolbar');
                $cmd04->signal_connect('activate', sub {
                        system ($command02);
                        });
                $cmd04->set_image($icon04);
                $cmdlist->append($cmd04);
                $icon06 = Gtk2::Image->new_from_stock('gtk-find','small-toolbar');
                $cmd06->signal_connect('activate', sub {
                        system ($command04);
                        });
                $cmd06->set_image($icon06);
                $cmdlist->append($cmd06);
                if ((`find "$bin_path" -iname "$command07"`) ne ""){
                        $cmd08 = Gtk2::ImageMenuItem->new('Ouvrir Marble');
                        $icon08 = Gtk2::Image->new_from_file("$icon/marble-22.png");
                        $cmd08->signal_connect('activate', sub {
                                system ($command07);
                                });
                        $cmd08->set_image($icon08);
                        $cmdlist->append($cmd08);
                }
                $cmdlist->append(Gtk2::SeparatorMenuItem->new());
                $icon05 = Gtk2::Image->new_from_stock('gtk-execute','large-toolbar');
                $cmd05->signal_connect('activate', sub {
                        system ($command03);
                        $advisory = $tempmsg00;
                        &msgbox1;
                        });
                if ((`find "$bin_path" -iname "$command05"`) ne ""){
                $cmd05->set_image($icon05);
                $cmdlist->append($cmd05);
                $icon07 = Gtk2::Image->new_from_file("$icon/icon1-22.png");
                $cmd07->signal_connect('activate', sub {
                        system ($command05);
                        });
                $cmd07->set_image($icon07);
                $cmdlist->append($cmd07);
                }
        # main menu container
        my $menubox = Gtk2::MenuBar->new();
        # submenu container
        my $menulist= Gtk2::MenuItem->new();


        #$img->set();
        my $imgloaded = "$icon/icon1-22-g.png";
        my $loader = Gtk2::Gdk::PixbufLoader->new;
        my $data = `cat "$imgloaded"`;
        $loader->write($data);
        $loader->close;
        my $pixbuf = $loader->get_pixbuf;
        my $iconscale = $pixbuf->scale_simple(16,16,'bilinear');
        my $img = Gtk2::Image->new_from_pixbuf($iconscale);
        $menulist->add($img);
        #add submenu to menubar
        $menulist->set_submenu($cmdlist);
        $menubox->append($menulist);
        $cmdbox->pack_start($menubox,FALSE,FALSE,0);
        # action
        #$vbox->pack_start($eventbox,TRUE,TRUE,0);
        $cmdbox->show_all();
        return $cmdbox;
}
sub advice {
my @entrymsg = ("$tempmsg01","$tempmsg02");
        foreach my $entry (@entrymsg) {
                if (`cat "$entry" |grep -o "PAS\|AUCUNE"` eq ''){
                        $advisory = $tempmsg02;
                }
                else {
                        $advisory = $tempmsg01;
                }
        }
        $count = (`cat -n "$advisory"|awk \'\{print \$1\}\'\|sed -n \'\$p\'`);
        print ($count);
        if ($count ge '5'){
               if ($count le '8'){
                        $advisory = $warnmsg;
                }
        }
}
sub msgbox1 {
        #$advisory = "$tempmsg";
        if ($advisory eq $warnmsg){
                my $txttitle = $advisory;
                $label1 = "\n<span foreground='red' size='x-large'>$txttitle</span>\n";
                $span = "$label1";
                $yesno = 'close';
        }
        else {
                my $msgtype = (`cat -n "$advisory"|awk \'\{print \$1\}\'\|sed -n \'\$p\'`);
                # NoWarning popup ()

                if ($msgtype le '5'){
                        my $txttitle= (`cat "$advisory"\|sed -n \'1p\'`); #\|sed -n \'s\/é\/e\/g\;s\/\[à\|â\]\/a\/g\;p\'`);
                        my $txtfield = (`cat "$advisory"\|sed -n \'2\,5p\'`); #\|sed -n \'s\/é\/e\/g\;s\/\[à\|â\]\/a\/g\;p\'`);
                        $txttitle =~ s/Ã©/é/g;$txttitle =~ s/Ã/à/g;$txttitle =~ s/Ã¢/â/g;
			$txtfield =~ s/Ã©/é/g;$txtfield =~ s/Ã/à/g;
                        $label1 = "\n<span foreground='yellow' size='large'>$txttitle</span>";
                        $label2 = "<span font='Gothic' size='large'>$txtfield</span>";
                        if ($txtfield ne ""){
                                $span = "$label1$label2";
                        }
                        else {
                                $span = "$label1";
                        }
                        $yesno = 'close';
                }
                # Warning popup (500,300)
                elsif($msgtype gt '5'){
                        my $txttitle = (`cat "$advisory"\|sed -n \'1p\'`);
                        my $txtadvisory = (`cat "$advisory"\|sed -n \'2p\'`);
                        my $txtfield = (`cat "$advisory"\|sed -n \'3\,7p\'`);
                        my $txtwarning = (`cat "$advisory"\|sed -n \'8p\'`);
                        $label1 = "\n<span foreground='yellow' size='x-large'>$txttitle</span>\n";
                        $label2 = "\n<span foreground='orange' weight='bold' size='large'>$txtadvisory</span>\n";
                        $label3 = "\n<span size='large'>$txtfield</span>";
                        $label4 = "\n<span foreground='green' weight='bold' size='large'>$txtwarning</span>\n";
                        $label5 = "\n<span weight='bold' size='medium'>Consulter l'image satellite ?</span>";
                        if ($txtwarning ne ""){
                                $span = "$label1$label2$label3$label4$label5";
                        }
                        $yesno = 'yes-no';
                }
        }
        my $txt = (); # (`echo -e \""$txttitle"\\n"$txtfield"\\n"$txtwarning""`);
        my $msg = Gtk2::MessageDialog->new_with_markup(undef,
                [qw/modal destroy-with-parent/],
                'warning',
                $yesno,
                $txt);
        $msg->set_markup("$span");
        $msg->set_size_request(490);
        $msg->set_border_width(12);
        $yes = $msg->run;
        if ($yes eq 'yes'){
                $msg->destroy;
        }
        else{
                $msg->destroy;
        }
}
